package com.evon.yardmanagement.ui.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.evon.yardmanagement.export.ImportDataManager;
import com.evon.yardmanagement.export.ImportableData;
import com.evon.yardmanagement.export.SectionExportableData;
import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.OEMService;
import com.evon.yardmanagement.service.SectionManagementService;
import com.evon.yardmanagement.service.YardManagementService;
import com.evon.yardmanagement.transformer.SectionTransformer;
import com.evon.yardmanagement.transformer.YardTransformer;
import com.evon.yardmanagement.ui.dto.OemDTO;
import com.evon.yardmanagement.ui.dto.SectionDTO;
import com.evon.yardmanagement.ui.dto.YardDTO;
import com.opensymphony.xwork2.ActionSupport;

public class SectionDetailsAction extends ActionSupport implements
		ApplicationAware, ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<SectionDTO> listSections;

	private List<YardDTO> listYard = new ArrayList<YardDTO>();

	private int selectedYardId;

	private Map<String, Object> globalData;

	private File sectionUploadFile;
	
	private boolean isYardSelected = true;

	private HttpServletRequest servletRequest;
	
	private List<OemDTO> oemList;
	
	private int oemType;
	
	public List<OemDTO> getOemList() {
		return oemList;
	}

	public void setOemList(List<OemDTO> oemList) {
		this.oemList = oemList;
	}

	public int getOemType() {
		return oemType;
	}

	public void setOemType(int oemType) {
		this.oemType = oemType;
	}

	public int getSelectedYardId() {
		return selectedYardId;
	}

	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}

	public List<YardDTO> getListYard() {
		return listYard;
	}

	public void setListYard(List<YardDTO> listYard) {
		this.listYard = listYard;
	}

	public SectionDetailsAction() {
		listSections = new ArrayList<SectionDTO>();
	}

	public List<SectionDTO> getListSections() {
		return listSections;
	}

	public void setListSections(List<SectionDTO> listSections) {
		this.listSections = listSections;
	}

	public String populate() {
		//isYardSelected = false;
		oemList = OEMService.getAllOem();
		
		// listYard = new YardManagementService().getAllYards();
		globalData.put("oemList", oemList);
		
		
		//If condition added for back button in create section
		if (getSelectedYardId()!=0) {
			populateSection();
		}
		/*
		 * Map map = ActionContext.getContext().getSession(); YardDTO yardDTO =
		 * (YardDTO)map.get("yard"); int yardId =
		 * yardDTO!=null?yardDTO.getId():0;
		 * System.out.println("yard id----"+yardId); if (yardId!=0) {
		 * SectionManagementService service = new SectionManagementService();
		 * List<Section> sections = service.getSectionByYardId(yardId);
		 * SectionDTO sectionDTO = null; for (Section section : sections) {
		 * sectionDTO = SectionTransformer.transformToDTO(section);
		 * listSections.add(sectionDTO); } }
		 */
		return "populate";
	}

	public String populateYard() {
		oemList = (List<OemDTO>)globalData.get("oemList");
		YardManagementService service = new YardManagementService();
		List<Yard> yards = service.getYardsByOemId(oemType);
		YardDTO yardDTO;
		int i = 0;
		for (Yard yard : yards) {
			yardDTO = YardTransformer.transform(yard);
			listYard.add(yardDTO);
			i++;
		}
		globalData.put("Yards", listYard);
		return "populate";
	}
	public String populateSection() {
		listSections = new ArrayList<SectionDTO>();
		listYard = (List<YardDTO>) globalData.get("Yards");
		oemList = (List<OemDTO>)globalData.get("oemList");
		int yardId = selectedYardId;
		SectionManagementService service = new SectionManagementService();
		List<Section> sectionList = service.getSectionByYardId(yardId);
		SectionDTO sectionDTO = null;
		for (Section section : sectionList) {
			sectionDTO = SectionTransformer.transformToDTO(section);
			listSections.add(sectionDTO);
		}
		return "populate";
	}

	public void setApplication(Map<String, Object> application) {
		this.globalData = application;
	}

	public String execute() {
		try {
			SectionExportableData data = new SectionExportableData(null);
			data.setFileToImport(sectionUploadFile);

			ImportDataManager<ImportableData> importManager = new ImportDataManager<ImportableData>(
					data);
			importManager.importDataFromExcel();

			new SectionManagementService().importSections(data.getData());
			System.err.println("sfsdfsfsfsfsf"
					+ this.sectionUploadFile.getName());
			addActionMessage("Imported Successfully");
			// String filePath = servletRequest.getSession().getServletContext()
			// .getRealPath("/");
			// System.out.println("Server path:" + filePath);
			// File fileToCreate = new File(filePath, this.userImageFileName);
			// FileUtils.copyFile(this.userImage, fileToCreate);
			populateSection();

		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public File getSectionUploadFile() {
		return sectionUploadFile;
	}

	public void setSectionUploadFile(File sectionUploadFile) {
		this.sectionUploadFile = sectionUploadFile;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public boolean isYardSelected() {
		return isYardSelected;
	}

	public void setYardSelected(boolean isYardNotSelected) {
		this.isYardSelected = isYardNotSelected;
	}
}
